package com.xiaocg.base.websocket;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;

/**
 * @author xiaocg
 */
public class TestWebSocket {
    private   SocketImpl socket;

    @Before
    public void connect() throws Exception{
        SocketImpl socket = new SocketImpl("ws://127.0.0.1:10050");
        socket.connectBlocking();
    }

    @Test
    public  void testSend(){
        System.out.println("---------------------------------");
        socket.send("-----------------ddddddddddddddddddddddddddd-------------------------");
    }

}

class SocketImpl extends WebSocketClient{

    public SocketImpl(String url) throws URISyntaxException {
        super(new URI(url));
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {

        for (Iterator<String> it = serverHandshake.iterateHttpFields();it.hasNext();
             ) {
            String key = it.next();
            System.out.println("key:"+key+",value:"+serverHandshake.getFieldValue(key));
        }
    }

    @Override
    public void onMessage(String s) {
        System.out.println("接收消息:"+s);
    }

    @Override
    public void onClose(int i, String s, boolean b) {

    }

    @Override
    public void onError(Exception e) {

    }
}
